package com.example.user

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.base.entity.CategoryEntityItem
import com.example.user.databinding.ItemCategoryBinding

/**
 * recyclerView原生适配器
 */
class CateYAdapter(val list:MutableList<CategoryEntityItem>):RecyclerView.Adapter<CateYAdapter.CateYViewHolder>() {

    class CateYViewHolder(itemView:View):ViewHolder(itemView){
        val binding = DataBindingUtil.bind<ItemCategoryBinding>(itemView)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CateYViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_category, null)
        return CateYViewHolder(view)
    }

    override fun getItemCount(): Int {
       return list.size
    }

    override fun onBindViewHolder(holder: CateYViewHolder, position: Int) {
        holder.binding?.info = list[position]
    }
    //增量刷新
    override fun onBindViewHolder(
        holder: CateYViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
       if(payloads.isEmpty()){
           onBindViewHolder(holder,position)
       }else{
           for (i in 0 until payloads.size){
               when(payloads[i].toString()){
                   "aaa"->{
                       holder.binding?.tv?.text = "1111"
                   }
                   "bbb"->{
                       holder.binding?.tv?.text = "22222"
                   }
               }
           }
       }
    }
}